﻿#include "MeasureSpecWidget.h"
#include "ui_MeasureSpec.h"
#include "b3mainwindow.h"

MeasureSpecWidget::MeasureSpecWidget(QWidget *parent, B3MainWindow *win) :
    QWidget(parent),
    ui(new Ui::MeasureSpec)
{
    ui->setupUi(this);
    clean();

    m_index = 0;

    m_sqlite = new Sqlite();

    m_win = win;

}

MeasureSpecWidget::~MeasureSpecWidget()
{
    delete ui;
}

void MeasureSpecWidget::on_toolButton_findDir_clicked()
{
    QString fileName = QFileDialog::getExistingDirectory(NULL,
                                                         u8"选择文件夹",
                                                         "D:/SQLite");
    ui->lineEdit_sqlPath->setText(fileName);
}

void MeasureSpecWidget::on_pushButton_createDataBase_clicked()
{
    if( ui->lineEdit_sqlPath->text().isEmpty())
    {
        QMessageBox::about(this, u8"提示", u8"请选择或输入目录");
        return;
    }
    QFileInfo fi(ui->lineEdit_sqlPath->text());
    if( !fi.isDir() )
    {
        QMessageBox::about(this, u8"提示", u8"目录不存在");
        int ret = QMessageBox::question(this,
                                        u8"提示",
                                        u8"目录不存在，是否创建",
                                        QMessageBox::Yes | QMessageBox::No,
                                        QMessageBox::Yes);
        if(ret == QMessageBox::Yes)
        {
            QDir dir;
            if(! dir.mkdir(ui->lineEdit_sqlPath->text()) )
            {
                QMessageBox::about(this, u8"提示", u8"目录创建失败");
                return;
            }
        }
        else
        {
            QMessageBox::about(this, u8"提示", u8"请重新选择目录");
            return;
        }
        return;
    }
    if( ui->lineEdit_sqlName->text().isEmpty())
    {
        QMessageBox::about(this, u8"提示", u8"数据库名称不能为空");
        return;
    }
    if( ui->spinBox->value() <= 0)
    {
        QMessageBox::about(this, u8"提示", u8"测点数量不能小于等于0");
        return;
    }
    //创建文件并创建表格写入信息
    QString fileName = ui->lineEdit_sqlPath->text() + "/" + ui->lineEdit_sqlName->text() + ".db";
    if( QFileInfo(fileName).isFile() )
    {
        int ret = QMessageBox::question(this,
                                        u8"提示",
                                        u8"文件已存在，是否读取？点击Yes读取，点击No覆盖",
                                        QMessageBox::Yes | QMessageBox::No,
                                        QMessageBox::Yes);
        if(ret == QMessageBox::Yes)
        {
            m_sqlite->setSqlData(fileName, ui->lineEdit_sqlName->text());
            ui->textEdit->clear();
            m_allMeasureSpecInfo.clear();
            if( m_sqlite->readMeasureSpec(m_allMeasureSpecInfo))
            {
                ui->spinBox->setValue(m_allMeasureSpecInfo.size());
                ui->textEdit->append(m_sqlite->readDescription());

                m_index = 0;
                ui->groupBox_MeasureSpec->setTitle(QString(u8"测点%1").arg( 1));
                ui->radioButton_CF->setChecked(!m_allMeasureSpecInfo[0].side);
                ui->radioButton_TFT->setChecked(m_allMeasureSpecInfo[0].side);
                ui->lineEdit_CL->setText(QString::number(m_allMeasureSpecInfo[0].CL,'f'));
                ui->lineEdit_UCL->setText(QString::number(m_allMeasureSpecInfo[0].UCL,'f'));
                ui->lineEdit_LCL->setText(QString::number(m_allMeasureSpecInfo[0].LCL,'f'));
                ui->lineEdit_USL->setText(QString::number(m_allMeasureSpecInfo[0].USL,'f'));
                ui->lineEdit_LSL->setText(QString::number(m_allMeasureSpecInfo[0].LSL,'f'));
                ui->lineEdit_startX->setText(QString::number(m_allMeasureSpecInfo[0].startX,'f'));
                ui->lineEdit_startY->setText(QString::number(m_allMeasureSpecInfo[0].startY,'f'));
                ui->lineEdit_startZ->setText(QString::number(m_allMeasureSpecInfo[0].startZ,'f'));
                ui->lineEdit_endX->setText(QString::number(m_allMeasureSpecInfo[0].endX,'f'));
                ui->lineEdit_endY->setText(QString::number(m_allMeasureSpecInfo[0].endY,'f'));
                ui->lineEdit_endZ->setText(QString::number(m_allMeasureSpecInfo[0].endZ,'f'));

                m_startScale = m_allMeasureSpecInfo[0].startScale;
                m_startLen = m_allMeasureSpecInfo[0].startLens;
                m_startIntensity_top = m_allMeasureSpecInfo[0].startIntensity_top;
                m_startIntensity_bottom = m_allMeasureSpecInfo[0].startIntensity_bottom;
                m_startExpoureTime = m_allMeasureSpecInfo[0].startExpoureTime;
                m_endScale = m_allMeasureSpecInfo[0].endScale;
                m_endLen = m_allMeasureSpecInfo[0].endLens;
                m_endIntensity_top = m_allMeasureSpecInfo[0].endIntensity_top;
                m_endIntensity_bottom = m_allMeasureSpecInfo[0].endIntensity_bottom;
                m_endExpoureTime = m_allMeasureSpecInfo[0].endExpoureTime;

                for(int i = 1; i <= m_allMeasureSpecInfo.size(); i++)
                {
                    qDebug() << i ;
                    qDebug() << "m_allMeasureSpecInfo[i-1].side" << m_allMeasureSpecInfo[i - 1].side;
                    ui->comboBox_MeasureID->addItem(QString(u8"测点%1").arg(i));

                }

                ui->groupBox_NewDB->setEnabled(false);
                ui->groupBox_MeasureSpec->setEnabled(true);
                return;
            }
            else
            {
                QMessageBox::about(this, u8"提示", u8"数据库读取失败");
                return;
            }
        }
        else
        {
            QFile file(fileName);
            file.remove();
        }
    }
    setData(fileName, ui->lineEdit_sqlName->text(), ui->spinBox->value(), ui->textEdit->toPlainText());
    if(!createDataBase())
    {
        QMessageBox::about(this, u8"提示", u8"数据路创建失败");
        return;
    }
    ui->groupBox_NewDB->setEnabled(false);
    ui->groupBox_MeasureSpec->setEnabled(true);
    m_allMeasureSpecInfo.clear();
    qDebug() << "ui->spinBox->value()" << ui->spinBox->value() ;
    for(int i = 1; i <= ui->spinBox->value(); i++)
    {
        qDebug() << i ;
        MeasureSpecInfo temp;
        temp.MeasureID = i;
        temp.side = 0;//CF面还是TFT面，CF面＝０，TFT面＝１
        temp.CL = 0;//中心线
        temp.UCL = 0;//控制上线
        temp.LCL = 0;//控制下线
        temp.USL = 0;//规格上线
        temp.LSL = 0;//规格下线
        temp.startX = 0;//视野起点
        temp.startY = 0;
        temp.startZ = 0;
        temp.startScale = 0;//没个像素对于的实际长度，单位mm
        temp.startLens = 0;//测量时的物镜
        temp.startIntensity_top = 0;//上光源亮度
        temp.startIntensity_bottom = 0;//下光源亮度
        temp.startExpoureTime = 0;//相机曝光时间
        temp.endX = 0;;//视野终点
        temp.endY = 0;;
        temp.endZ = 0;;
        temp.endScale = 0;;//没个像素对于的实际长度，单位mm
        temp.endLens = 0;;//测量时的物镜
        temp.endIntensity_top = 0;;//上光源亮度
        temp.endIntensity_bottom = 0;;//下光源亮度
        temp.endExpoureTime = 0;;//相机曝光时间

        m_allMeasureSpecInfo.append(temp);
    }
    for(int i = 1; i <= ui->spinBox->value(); i++)
    {
        qDebug() << i ;
        ui->comboBox_MeasureID->addItem(QString(u8"测点%1").arg(i));
    }
    ui->comboBox_MeasureID->setCurrentIndex(0);
    ui->groupBox_MeasureSpec->setTitle(QString(u8"测点%1").arg(ui->comboBox_MeasureID->currentIndex() + 1));
}


void MeasureSpecWidget::on_pushButton_GetStartPos_clicked()
{
    double pos_mm[4];
    m_win->getEncodePosition(pos_mm);

    if( checkImage(m_win->getImage())== QDialog::Accepted)
    {
        ui->lineEdit_startX->setText( QString::number(pos_mm[0],'f'));
        ui->lineEdit_startY->setText( QString::number(pos_mm[1],'f'));
        ui->lineEdit_startZ->setText( QString::number(pos_mm[2],'f'));
        m_startScale = m_win->getScale();
        m_startLen = m_win->getLens();
        m_startIntensity_top = m_win->getIntensity_top();
        m_startIntensity_bottom = m_win->getIntensity_bottom();
        m_startExpoureTime = m_win->getExpoureTime();
    }
}

void MeasureSpecWidget::on_pushButton_GetEndPos_clicked()
{
    double pos_mm[4];
    m_win->getEncodePosition(pos_mm);
    if( checkImage(m_win->getImage())== QDialog::Accepted)
    {
        ui->lineEdit_endX->setText( QString::number(pos_mm[0],'f'));
        ui->lineEdit_endY->setText( QString::number(pos_mm[1],'f'));
        ui->lineEdit_endZ->setText( QString::number(pos_mm[2],'f'));
        m_endScale = m_win->getScale();
        m_endLen = m_win->getLens();
        m_endIntensity_top = m_win->getIntensity_top();
        m_endIntensity_bottom = m_win->getIntensity_bottom();
        m_endExpoureTime = m_win->getExpoureTime();
    }
}

void MeasureSpecWidget::on_pushButton_Write_clicked()
{
    m_sqlite->clearMeasureSpec();
    on_comboBox_MeasureID_currentIndexChanged(ui->comboBox_MeasureID->currentIndex());
    for(int i = 0; i < m_allMeasureSpecInfo.size(); i++)
    {
        if(!m_sqlite->writeSpec2Sqlite(m_allMeasureSpecInfo[i]))
        {
            QMessageBox::warning(this, u8"警告", u8"数据库写入失败！");
            return;
        }
    }
    QMessageBox::about(this, u8"提示", u8"全部测点保存完毕");
    ui->pushButton_Over->setEnabled(true);
}

void MeasureSpecWidget::on_comboBox_MeasureID_currentIndexChanged(int index)
{
    if(index < 0) return;
    if(index >= m_allMeasureSpecInfo.size())return;
    qDebug() << "on_comboBox_MeasureID_currentIndexChanged " << index << m_index;
    if(ui->radioButton_CF->isChecked() == false && ui->radioButton_TFT == false)
    {
        QMessageBox::warning(this, u8"警告", u8"未选择测量面（CF or TFT）！");
        return;
    }

    m_allMeasureSpecInfo[m_index].MeasureID = m_index + 1;
    m_allMeasureSpecInfo[m_index].side = ui->radioButton_TFT->isChecked();
    m_allMeasureSpecInfo[m_index].CL = ui->lineEdit_CL->text().toDouble();
    m_allMeasureSpecInfo[m_index].UCL = ui->lineEdit_UCL->text().toDouble();
    m_allMeasureSpecInfo[m_index].LCL = ui->lineEdit_LCL->text().toDouble();
    m_allMeasureSpecInfo[m_index].USL = ui->lineEdit_USL->text().toDouble();
    m_allMeasureSpecInfo[m_index].LSL = ui->lineEdit_LSL->text().toDouble();
    m_allMeasureSpecInfo[m_index].startX = ui->lineEdit_startX->text().toDouble();
    m_allMeasureSpecInfo[m_index].startY = ui->lineEdit_startY->text().toDouble();
    m_allMeasureSpecInfo[m_index].startZ = ui->lineEdit_startZ->text().toDouble();
    m_allMeasureSpecInfo[m_index].startScale = m_startScale;
    m_allMeasureSpecInfo[m_index].startLens = m_startLen;
    m_allMeasureSpecInfo[m_index].startIntensity_top = m_startIntensity_top;
    m_allMeasureSpecInfo[m_index].startIntensity_bottom = m_startIntensity_bottom;
    m_allMeasureSpecInfo[m_index].startExpoureTime = m_startExpoureTime;
    m_allMeasureSpecInfo[m_index].endX = ui->lineEdit_endX->text().toDouble();
    m_allMeasureSpecInfo[m_index].endY = ui->lineEdit_endY->text().toDouble();
    m_allMeasureSpecInfo[m_index].endZ = ui->lineEdit_endZ->text().toDouble();
    m_allMeasureSpecInfo[m_index].endScale = m_endScale;
    m_allMeasureSpecInfo[m_index].endLens = m_endLen;
    m_allMeasureSpecInfo[m_index].endIntensity_top = m_endIntensity_top;
    m_allMeasureSpecInfo[m_index].endIntensity_bottom = m_endIntensity_bottom;
    m_allMeasureSpecInfo[m_index].endExpoureTime = m_endExpoureTime;

    ui->groupBox_MeasureSpec->setTitle(QString(u8"测点%1").arg(index + 1));

    ui->radioButton_CF->setChecked(!m_allMeasureSpecInfo[index].side);
    ui->radioButton_TFT->setChecked(m_allMeasureSpecInfo[index].side);
    ui->lineEdit_CL->setText(QString::number(m_allMeasureSpecInfo[index].CL,'f'));
    ui->lineEdit_UCL->setText(QString::number(m_allMeasureSpecInfo[index].UCL,'f'));
    ui->lineEdit_LCL->setText(QString::number(m_allMeasureSpecInfo[index].LCL,'f'));
    ui->lineEdit_USL->setText(QString::number(m_allMeasureSpecInfo[index].USL,'f'));
    ui->lineEdit_LSL->setText(QString::number(m_allMeasureSpecInfo[index].LSL,'f'));
    ui->lineEdit_startX->setText(QString::number(m_allMeasureSpecInfo[index].startX,'f'));
    ui->lineEdit_startY->setText(QString::number(m_allMeasureSpecInfo[index].startY,'f'));
    ui->lineEdit_startZ->setText(QString::number(m_allMeasureSpecInfo[index].startZ,'f'));
    ui->lineEdit_endX->setText(QString::number(m_allMeasureSpecInfo[index].endX,'f'));
    ui->lineEdit_endY->setText(QString::number(m_allMeasureSpecInfo[index].endY,'f'));
    ui->lineEdit_endZ->setText(QString::number(m_allMeasureSpecInfo[index].endZ,'f'));

    m_startScale = m_allMeasureSpecInfo[index].startScale;
    m_startLen = m_allMeasureSpecInfo[index].startLens;
    m_startIntensity_top = m_allMeasureSpecInfo[index].startIntensity_top;
    m_startIntensity_bottom = m_allMeasureSpecInfo[index].startIntensity_bottom;
    m_startExpoureTime = m_allMeasureSpecInfo[index].startExpoureTime;
    m_endScale = m_allMeasureSpecInfo[index].endScale;
    m_endLen = m_allMeasureSpecInfo[index].endLens;
    m_endIntensity_top = m_allMeasureSpecInfo[index].endIntensity_top;
    m_endIntensity_bottom = m_allMeasureSpecInfo[index].endIntensity_bottom;
    m_endExpoureTime = m_allMeasureSpecInfo[index].endExpoureTime;

    m_index = index;
}

void MeasureSpecWidget::on_lineEdit_CL_editingFinished()
{
    ui->lineEdit_LSL->setText(QString::number(ui->lineEdit_CL->text().toDouble() - 0.2));
    ui->lineEdit_LCL->setText(QString::number(ui->lineEdit_CL->text().toDouble() - 0.1));
    ui->lineEdit_UCL->setText(QString::number(ui->lineEdit_CL->text().toDouble() + 0.1));
    ui->lineEdit_USL->setText(QString::number(ui->lineEdit_CL->text().toDouble() + 0.2));
}

void MeasureSpecWidget::clean()
{
    m_index = 0;
    ui->spinBox->setValue(4);
    ui->lineEdit_CL->setText("");
    ui->lineEdit_LCL->setText("");
    ui->lineEdit_LSL->setText("");
    ui->lineEdit_UCL->setText("");
    ui->lineEdit_USL->setText("");
    ui->lineEdit_startX->setText("");
    ui->lineEdit_startY->setText("");
    ui->lineEdit_startZ->setText("");
    ui->lineEdit_endX->setText("");
    ui->lineEdit_endY->setText("");
    ui->lineEdit_endZ->setText("");
    ui->radioButton_CF->setChecked(true);
    ui->radioButton_TFT->setChecked(true);
    ui->groupBox_NewDB->setEnabled(true);
    ui->groupBox_MeasureSpec->setEnabled(false);
    ui->groupBox_MeasureSpec->setTitle(u8"测点");
    ui->comboBox_MeasureID->clear();
}

void MeasureSpecWidget::setData(QString sqlPath, QString panelName, int measureNumber, QString description)
{
    m_sqlPath = sqlPath;
    m_panelName = panelName;
    m_measureNumber = measureNumber;

    m_sqlite->setCreateTableData(sqlPath, panelName, measureNumber, description);
}

bool MeasureSpecWidget::createDataBase()
{
    if(!m_sqlite->createDataBaseFile())
    {
        QMessageBox::about(this, u8"提示", u8"创建数据库失败");
        return false;
    }
    if(!m_sqlite->createDataBaseTable())
    {
        QMessageBox::about(this, u8"提示", u8"创建表格失败");
        return false;
    }
    return true;
}

bool MeasureSpecWidget::writeMeasureSpec()
{
    if(ui->radioButton_CF->isChecked() == false && ui->radioButton_TFT == false)
    {
        QMessageBox::warning(this, u8"警告", u8"未选择测量面（CF or TFT）！");
        return false;
    }
    m_measureSpecInfo.side = ui->radioButton_TFT->isChecked();
    m_measureSpecInfo.CL = ui->lineEdit_CL->text().toDouble();
    m_measureSpecInfo.UCL = ui->lineEdit_UCL->text().toDouble();
    m_measureSpecInfo.LCL = ui->lineEdit_LCL->text().toDouble();
    m_measureSpecInfo.USL = ui->lineEdit_USL->text().toDouble();
    m_measureSpecInfo.LSL = ui->lineEdit_LSL->text().toDouble();

    m_measureSpecInfo.startX = ui->lineEdit_startX->text().toDouble();
    m_measureSpecInfo.startY = ui->lineEdit_startY->text().toDouble();
    m_measureSpecInfo.startZ = ui->lineEdit_startZ->text().toDouble();
    m_measureSpecInfo.startScale = m_startScale;
    m_measureSpecInfo.startLens = m_startLen;
    m_measureSpecInfo.startIntensity_top = m_startIntensity_top;
    m_measureSpecInfo.startIntensity_bottom = m_startIntensity_bottom;
    m_measureSpecInfo.startExpoureTime = m_startExpoureTime;

    m_measureSpecInfo.endX = ui->lineEdit_endX->text().toDouble();
    m_measureSpecInfo.endY = ui->lineEdit_endY->text().toDouble();
    m_measureSpecInfo.endZ = ui->lineEdit_endZ->text().toDouble();
    m_measureSpecInfo.endScale = m_endScale;
    m_measureSpecInfo.endLens = m_endLen;
    m_measureSpecInfo.endIntensity_top = m_endIntensity_top;
    m_measureSpecInfo.endIntensity_bottom = m_endIntensity_bottom;
    m_measureSpecInfo.endExpoureTime = m_endExpoureTime;

    return m_sqlite->writeSpec2Sqlite(m_measureSpecInfo);
}

int MeasureSpecWidget::checkImage(QImage img)
{
    QRect roi;
    roi.setRect(0.1 * img.width(), 0.1 * img.height(), 0.8 * img.width(), 0.8 * img.height());
    img = m_win->getImage().copy(roi);
    CheckPositionDialog m_CheckPosition;
    m_CheckPosition.setImage(img);
    return m_CheckPosition.exec();
}


void MeasureSpecWidget::on_pushButton_GotoSrtart_clicked()
{

    emit goToXYZ( ui->lineEdit_startX->text().toDouble(),
                  ui->lineEdit_startY->text().toDouble(),
                  ui->lineEdit_startZ->text().toDouble(),
                  100 );
}

void MeasureSpecWidget::on_pushButton_GotoEnd_clicked()
{
    emit goToXYZ( ui->lineEdit_endX->text().toDouble(),
                  ui->lineEdit_endY->text().toDouble(),
                  ui->lineEdit_endZ->text().toDouble(),
                  100);
}
